package ca.ericslandry.client.mvp.view;

import ca.ericslandry.client.GwtUtils;
import ca.ericslandry.client.mvp.presenter.IssuePresenter;
import ca.ericslandry.client.resource.MyConstants;
import ca.ericslandry.client.widget.LocationPicker;
import ca.ericslandry.client.widget.RichTextToolbar;
import ca.ericslandry.shared.domain.Issue;
import ca.ericslandry.shared.domain.User;
import ca.ericslandry.shared.util.GeoHash;

import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.logical.shared.ResizeEvent;
import com.google.gwt.event.logical.shared.ResizeHandler;
import com.google.gwt.maps.client.MapWidget;
import com.google.gwt.maps.client.control.SmallZoomControl;
import com.google.gwt.maps.client.geom.LatLng;
import com.google.gwt.maps.client.overlay.Marker;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHTML;
import com.google.gwt.user.client.ui.HasValue;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RichTextArea;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class IssueView extends BaseView implements IssuePresenter.Display, ResizeHandler {
	private Button save = new Button(MyConstants.INSTANCE.save());
	private TextBox title = new TextBox();
	private RichTextArea description = new RichTextArea();
	private RichTextToolbar toolbar = new RichTextToolbar(description);
	private SimplePanel headerPanel = new SimplePanel();
	private VerticalPanel issueContentPanel = new VerticalPanel();
	private ScrollPanel issueScrollPanel = new ScrollPanel(issueContentPanel);
	private LocationPicker locationPicker = new LocationPicker();
	private VerticalPanel panel = new VerticalPanel();

	public IssueView() {
		panel.setWidth("100%");
		panel.add(headerPanel);
		issueScrollPanel.setStyleName("scroll");
		panel.add(issueScrollPanel);
		issueContentPanel.setWidth("100%");
		issueContentPanel.setSpacing(10);
		Window.addResizeHandler(this);
	}

	public Widget asWidget() {
		return panel;
	}

	@Override
	public HasClickHandlers getSave() {
		return save;
	}

	@Override
	public HasValue<String> getTitleValue() {
		return title;
	}

	@Override
	public HasHTML getDescription() {
		return description;
	}

	private void reset() {
		headerPanel.clear();
		issueContentPanel.clear();
	}

	@Override
	public void createIssue() {
		reset();

		// Add page header
		Label label = new Label(MyConstants.INSTANCE.newIssue());
		label.setStyleName("header");
		headerPanel.add(label);

		// Add issue title
		Label titleLabel = new Label(MyConstants.INSTANCE.title());
		titleLabel.setStyleName("sectionHeader");
		issueContentPanel.add(titleLabel);
		title.setWidth("100%");
		issueContentPanel.add(title);

		// Add issue description
		Label descLabel = new Label(MyConstants.INSTANCE.description());
		descLabel.setStyleName("sectionHeader");
		issueContentPanel.add(descLabel);
		Grid grid = new Grid(2, 1);
		grid.setWidth("100%");
		toolbar.setWidth("100%");
		grid.setWidget(0, 0, toolbar);
		grid.setStyleName("richText");
		description.setSize("100%", "14em");
		grid.setWidget(1, 0, description);
		issueContentPanel.add(grid);
		Label locationLabel = new Label("Pick a Location");
		locationLabel.setStyleName("sectionHeader");
		issueContentPanel.add(locationLabel);
		issueContentPanel.add(locationPicker);

		// Add control buttons
		issueContentPanel.add(save);
		onWindowResized(Window.getClientWidth(), Window.getClientHeight());

	}

	@Override
	public void showIssue(Issue issue) {
		reset();
		Label label = new Label(issue.getTitle());
		label.setStyleName("header");
		headerPanel.add(label);
		HTML html = new HTML(issue.getDescription());
		DockPanel panel = new DockPanel();
		panel.setSpacing(10);
		panel.add(html, DockPanel.NORTH);
		panel.add(getMapWidget(issue), DockPanel.WEST);
		panel.add(getCreatorWidget(issue), DockPanel.EAST);
		issueContentPanel.add(panel);
		onWindowResized(Window.getClientWidth(), Window.getClientHeight());
	}

	private Widget getMapWidget(Issue issue) {
		MapWidget map = new MapWidget();
		map.setPixelSize(300, 200);
		map.addControl(new SmallZoomControl());
		GeoHash geohash = GeoHash.fromGeohashString(issue.getGeohash());
		LatLng point = geohash.getPoint().toLatLng();
		map.setCenter(point, 12);
		Marker marker = new Marker(point);
		map.clearOverlays();
		map.addOverlay(marker);
		return map;
	}

	private Widget getCreatorWidget(Issue issue) {
		String creationDate = GwtUtils.prettyDate(issue.getCreationDate());
		User creator = issue.getCreator();
		String displayName = creator.getDisplayName();
		// TODO: Make name linkable and show user details
		creationDate += "<br/>" + MyConstants.INSTANCE.by() + displayName;
		HorizontalPanel panel = new HorizontalPanel();
		panel.setSpacing(5);
		HTML html = new HTML(creationDate);
		panel.add(html);
		return panel;
	}

	@Override
	public void onResize(ResizeEvent event) {
		onWindowResized(event.getWidth(), event.getHeight());
	}

	private void onWindowResized(int width, int height) {
		int y = issueScrollPanel.getAbsoluteTop();
		int newHeight = height - y - 9;
		issueScrollPanel.setHeight(newHeight + "px");
	}

	@Override
	public HasValue<String> getGeohash() {
		return null;
	}
}